#!/sbin/openrc-run

extra_commands="configtest reload"

depend() {
	need net
}

start() {
	configtest || return 1
	ebegin "Starting tokyo tyrant"
	start-stop-daemon --start --exec /usr/bin/ttserver -- \
		-pid ${TT_PID} -dmn -host ${TT_HOST} -log ${TT_LOG} \
		-port ${TT_PORT} ${TT_OPTS} ${TT_DB}
	eend $? "Failed to start tokyo tyrant"
}

stop() {
	configtest || return 1
	ebegin "Stopping tokyo tyrant"
	start-stop-daemon --stop --pidfile ${TT_PID}
	eend $? "Failed to stop tokyo tyrant"
	rm -f ${TT_PID}
}

reload() {
	configtest || return 1
	ebegin "Gracefully restarting tokyo tyrant"
	kill -HUP `cat ${TT_PID}` &>/dev/null
	eend $? "Failed to reload tokyo tyrant"
}

configtest() {
	if [ -z "${TT_DB}" ] || [ -z "${TT_PORT}" ] ; then
		eerror "Please check your configuration"
		eend $?
	fi
}
